﻿using System.Text;

namespace TPXSDK.NET.Mission.Instruction;

internal class InstDelay : Tokenizer
{
    public InstDelay(TokenizerAdmin admin)
    {
        Admin = admin;
        KeyName = "delay";
        KeyValue = (int)FlyCmdId.CMD_DELAY;
        Usage = "delay duration";
        Description = @"pauses the thread for duration (unit ms). The thread does not run any commands while paused.";
    }

    public override bool Process()
    {
        var flyCmp = Admin?.Compiler; if (null == flyCmp) return false;
        var tokens = flyCmp.GetLineTokens();

        if (null == tokens || tokens.Count != 2)
        {
            flyCmp.ErrorMsg = $"{KeyName} Invalid Usage";
            return false;
        }

        var cmd = new FlyCmdItem
        {
            Id = (byte)KeyValue,
            LineNum = tokens[0].Row
        };
        tokens.RemoveAt(0);// Remove Flycmd Name
        if (tokens[0].Type != TokenType.NUMBER)
        {
            flyCmp.ErrorMsg = $"{KeyName} Invalid param";
            return false;
        }

        cmd.P3.U32Val = Convert.ToUInt32(tokens[0].Name);
        flyCmp.CmdList.Add(cmd);
        return true;
    }

    public override void Reverse(FlyCmdItem cmd, StringBuilder sb)
    {
        sb.AppendLine($"{KeyName} {cmd.P3.U32Val} // ms");
    }

}
